package com.socool.sinofaith.ui;

import java.util.ArrayList;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.dao.SifTaskQuestionDao;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.SifTaskQuestionBean;
import com.socool.sinofaith.model.ViewQuestionBean;
import com.socool.sinofaith.model.ViewQuestionBean.Question;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;

public class PreviewQuestionActivity extends BaseActivity implements
		CenterViewFactory, SpinnerViewFactory {
	ActionBarHelperBase mActionBarHelperBase;

	private LinearLayout linearLayout;

	private SifTaskQuestionBean taskQuestionBean;

	private ArrayList<SifTaskQuestionBean> questionBeans;

	private String Task_Name, taskid, Template_ID, Answer_ID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_questiondetail);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);

		linearLayout = (LinearLayout) findViewById(R.id.prarms_layout);
		
		taskid = getIntent().getStringExtra("taskid");
		Template_ID = getIntent().getStringExtra("Template_ID");
		Answer_ID=getIntent().getStringExtra("Answer_ID");
		previewQuestion();
		Task_Name = getIntent().getStringExtra("taskName");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.common, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public View createView() {
		TextView tv = new TextView(this);
		tv.setText(Task_Name);
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		tv.setSingleLine(true);
		tv.setMaxEms(10);
		tv.setEllipsize(TruncateAt.END);
		return tv;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated
															// method stub
		if (item.getItemId() == R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View createSpinnerView() {
		ImageView imageview = new ImageView(this);
		return imageview;
	}

	void previewQuestion(){
		final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		linearLayout.removeAllViews();
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		new AsyncTask<Void, Void, ArrayList<SifTaskQuestionBean>>() {

			@Override
			protected ArrayList<SifTaskQuestionBean> doInBackground(
					Void... arg0) {
				SifTaskQuestionDao taskQuestionDao = new SifTaskQuestionDao(
						PreviewQuestionActivity.this);
				questionBeans = taskQuestionDao.queryBySql("select * from "
						+ SifTaskQuestionBean.TABLENAME
						+ " where Template_ID='" + Template_ID
						+ "' and Task_ID='" + taskid + "'"
						+ " order by cast(OrderIndex as INTEGER)");
				return questionBeans;
			}

			@Override
			protected void onPostExecute(
					ArrayList<SifTaskQuestionBean> result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				if (result != null && result.size() > 0) {
					for(int i=0;i<result.size();i++){
						taskQuestionBean=result.get(i);
						TextView textView = new TextView(PreviewQuestionActivity.this);
						// 改变默认的单行模式
						textView.setSingleLine(false);
						// 水平滚动设置为False
						textView.setHorizontallyScrolling(false);
						textView.setLayoutParams(params);
						textView.setTextSize(18);
						textView.setText(taskQuestionBean.getOrderIndex()+ "、"
								+ taskQuestionBean.getQuestion());
						linearLayout.addView(textView);
					}
				}else{
					getNetData();
				}
			}
		}.execute();
		
	}
	


	@SuppressWarnings({ "unchecked", "rawtypes" })
	void getNetData() {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.viewQuestion_url);
		params.put("did", SinofaithUtil.did);
		params.put("answerid", Answer_ID);
		Log.v("qqqq", ConstansJsonUrl.REQUEST_URL + params);
		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {

			@Override
			public void onStart() {
				super.onStart();
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				Log.v("qqqq", t.toString());
				try {
					ViewQuestionBean viewQuestionBean = HttpJsonAdapter
							.getInstance().get(t.toString(),
									ViewQuestionBean.class);
					if (viewQuestionBean.getResult().equals("0")) {

						if (null != viewQuestionBean.getQuestionList()
								&& viewQuestionBean.getQuestionList().size() > 0) {

							ArrayList<Question> questions = viewQuestionBean
									.getQuestionList();
							LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.FILL_PARENT,
									LinearLayout.LayoutParams.FILL_PARENT);
							linearLayout.removeAllViews();
							linearLayout.setOrientation(LinearLayout.VERTICAL);
							if (null != questions && questions.size() > 0) {
								for (int i = 0; i < questions.size(); i++) {
									Question question = questions.get(i);
									TextView textView = new TextView(PreviewQuestionActivity.this);
									// 改变默认的单行模式
									textView.setSingleLine(false);
									// 水平滚动设置为False
									textView.setHorizontallyScrolling(false);
									textView.setLayoutParams(params);
									textView.setTextSize(18);
									textView.setText(question.getOrderIndex()+ "、"
											+ question.getQuestion());
									linearLayout.addView(textView);
								}

							}
						}

					} else {
						Toast.makeText(PreviewQuestionActivity.this,
								viewQuestionBean.getDesc(), Toast.LENGTH_LONG)
								.show();
						PreviewQuestionActivity.this.finish();
					}
				} catch (Exception e) {
					Toast.makeText(PreviewQuestionActivity.this, e.getMessage(),
							Toast.LENGTH_LONG).show();
					PreviewQuestionActivity.this.finish();
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(PreviewQuestionActivity.this, strMsg,
						Toast.LENGTH_LONG).show();
			}

		});
	}
}
